#ifndef __VEHICLE_H__
#define __VEHICLE_H__

#define TRY_AUTO_PROTOCOL	1
#define GET_PROTOCOL			2
#define TRY_PROTOCOL			3
#define SET_PROTOCOL			4
#define GET_PROTOCOLNUMBER	5
#define GET_VOLTAGE			6
#define SET_BRD_Terminal		7
#define SET_BRTONVM			8
#define AT_RESET				9
#define SET_HEADER				10

#define VEHICLE_CONN_TRIES	60		// 60 Times
#define VEHICLE_INITIAL_WAITTING 240 // 5 minutes
#define VEHICLE_IS_STOP 180	// Wait for 5 minutes Before MCU Sleep

#define SUPPORT_PROTOCOLS_NUM	11


extern const char OBD_PROTOCL_NAME[SUPPORT_PROTOCOLS_NUM][40];
extern const char PROTOCOLNAME[SUPPORT_PROTOCOLS_NUM][15];

struct vehicle_information {
	unsigned char protocol_type;
	unsigned char protocol_num;
	unsigned char protocol_name[15];
	unsigned char VIN[17];
	unsigned char SN[8];
	unsigned char CID[16];
	unsigned char PID00[4];
	unsigned char PID20[4];
	unsigned char PID40[4];
	unsigned char PID90[4];
	unsigned char gas_cal;
	unsigned char fuel_cal[2];
	unsigned char POI;
	unsigned char dist_cal[2];
	unsigned char CALID[8];
};

struct vehicle_status {
	unsigned char ConfigFileOK;
	unsigned char Voltage;
	unsigned char VehicleKeyOn;
	unsigned char VehicleEnginStart;
	unsigned char FuelSystemStatus;			// 01 03	
	// [A0: Open loop due to insufficient engine temp]
	// [A1: Close loop, using oxygen sensor feedback to determine fuel mix]				
	// [A2: Open loop due to engine load OR fuel cut due to deceleration]
	// [A3: Open loop due to system failure]
	//	[A4: Closed loop, using at least one oxygen sensor but there is a 
	//			fault in the feedback system]
	unsigned char Engine_Load;				// 01 04	[A * 100 / 255]
	unsigned char Cool_Temp;					// 01 05	[A - 40]
	unsigned char ECU_STFuelTrim;			// 01 06	[(A-128) * 100 / 128]
	unsigned char ECU_LTFuelTrim;			// 01 07	[(A-128) * 100 / 128]
	unsigned char MAP;							// 01 0B	[A]
	unsigned char VehicleEnginRPM[2];		// 01 0C	[((A * 256) + B) / 4]
	unsigned char VehicleSpeed;				// 01 0D	[A]
	unsigned char IntakeAirTemp;			// 01 0F	[A - 40]
	unsigned char AirFlowRate[2];			// 01 10	[((A * 256)+B) / 100]
	unsigned char Pad_Position;				// 01 11	[A * 100 / 255]
	unsigned char Dist_Cal21[2];			// 01 21	[A * 256 + B]
	unsigned char Dist_Cal31[2];			// 01 31	[A * 256 + B] 
	unsigned char Fuel_LV;					// 01 2F	[A * 100 / 255]	
	unsigned char EngineFuelRate[2];			// 01 5E	[(A * 256 + B) * 0.05]
};

struct fuel_rate_para{
	int rpm;					// 01 0C
	int CONSTANT;			// CONSTANT
	int VE;					// CONSTANT	7
	int map;					// 01 0B
	int ED;					// CONSTANT	24
	int R;						// CONSTANT	8314
	int iat;					// 01 0F
	int MAIR;					//	CONSTANT	2897
	int maf;					//	01 10
	int AFR;					// CONSTANT	1464
	int FUELP;				// CONSTANT	74
	int stft;					// 01 06
	int ltft;					// 01 07
	int speed;				// 01 0D
	int efr;					// 01 5E
	int fuelrate;			// 1st Calculation result
	int fuelrate100;		// 2nd Calculation result
};

struct drive_action{
	int throttlePOS;		// 01 11
};



struct vehicle_cmd {
	int (*CheckConnect)(int fd, unsigned char *rxbuf, unsigned char alpha);

	int (*GetPIDSupport)(int fd, unsigned char *rxbuf, \
											unsigned char alpha, unsigned char * data);

	int (*GetVIN)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
	int (*GetCALID)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
	int (*GetRPM)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetEFR)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetMAP)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetFUELLV)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);											
	int (*GetIAT)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetMAF)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetSTFT)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetLTFT)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetSPEED)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetENGINELOAD)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetCOOLTEMP)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetPADPOS)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetFR)(struct fuel_rate_para *frp);
	int (*GetDIST21)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int (*GetDIST31)(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status * vs);
	int(*GetMultiMAP)(int fd, unsigned char *rxbuf, unsigned char alpha, \
								struct vehicle_status *, struct fuel_rate_para *);
	int(*GetMultiMAF)(int fd, unsigned char *rxbuf, unsigned char alpha, \
								struct vehicle_status *, struct fuel_rate_para *);
	int(*GetMultiEFR)(int fd, unsigned char *rxbuf, unsigned char alpha, \
								struct vehicle_status *, struct fuel_rate_para *);
	int(*GetMultiDriveBehavior)(int fd, unsigned char *rxbuf,	\
								unsigned char alpha, 	struct vehicle_status *);
};

#endif
